package com.meteor.controller;

import com.alibaba.fastjson.JSONObject;
import com.meteor.server.WebSocket;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author lynn
 * @date 2024/1/24
 */
@RestController
@RequestMapping("/api/msg")
public class WebSocketController {

    @Resource
    private WebSocket webSocket;

    @RequestMapping("/all/{msg}") // 将消息发送到/topic/greetings路径下
    public void all(@PathVariable String msg) {
        //创建业务消息信息
        JSONObject obj = new JSONObject();
        obj.put("msg", msg);//消息内容
        //全体发送
        webSocket.sendAllMessage(obj.toJSONString());
    }
    @RequestMapping("/{userId}/{msg}") // 将消息发送到/topic/greetings路径下
    public void sendUser(@PathVariable String userId,@PathVariable String msg) {
        //创建业务消息信息
        JSONObject obj = new JSONObject();
        obj.put("msg", msg);//消息内容
        webSocket.sendOneMessage(userId, obj.toJSONString());
    }

    @RequestMapping("/notice")
    public void notice() {
        //创建业务消息信息
        JSONObject obj = new JSONObject();
        obj.put("msg", "11111");//消息内容
        //全体发送
        webSocket.sendAllMessage(obj.toJSONString());
    }

}
